//
//  ARRequestModel.m
//  BrushFace
//
//  Created by timer_q on 16/1/17.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "ARRequestModel.h"
#import <AFNetworking.h>
@implementation ARRequestModel
DEF_SINGLETON(ARRequestModel)

#pragma mark--------xuxuejiao
//添加了一个block，获取优惠券的数量

//换了个接口

- (void)youhuiquanCountAndCB:(void(^)(id output))CB
{
    NSString *requestStr = [NSString stringWithFormat:@"%@/hope/getlendcouponslist.json",SERVICE_ADRESS];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:requestStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsondict = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        CB(jsondict);
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
}
#pragma mark-----------------------

- (NSString *)fetchBaseInfoRequestStr{
    
    NSMutableString *str = [[NSMutableString alloc] init];
    [str appendFormat:@"%@/",SERVICE_ADRESS];
    switch (self.baseType) {
        case 0:
        {
            switch (self.pageType) {
                case 0:
                case 11:
                case 12:
                case 1://借款中
                {
                    [str appendString:@"hope/getborrowinpublish4app.json"];
                }
                    break;
                    case 2://已满标
                    case 700:
                {
                    [str appendString:@"hope/getborrowinfull4app.json"];
                }
                    break;
                case 3:
                case 13:
                case 9:
                case 4://还款中
                {
                    [str appendString:@"hope/getborrowinrepaying4app.json"];
                }
                    break;
                case 5://已逾期
                {
                    [str appendString:@"hope/getborrowintimeout4app.json"];
                }
                    break;
                    case 7://已流标
                    case 6:
                {
                    [str appendString:@"hope/getborrowinflow4app.json"];
                }
                    break;
                case 10:
                case 8://已还款
                {
                    [str appendString:@"hope/getborrowinrepayed4app.json"];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 1:
        {
            switch (self.pageType) {
                case 0:
                case 11:
                case 12:
                case 1://借款中
                {
                    [str appendString:@"hope/getborrowoutpublish4app.json"];
                }
                    break;
                    case 2://已满标
                    case 700:
                {
                    [str appendString:@"hope/getborrowoutfull4app.json"];
                }
                    break;
                case 3:
                case 13:
                case 9:
                case 4://还款中
                {
                    [str appendString:@"hope/getborrowoutrepaying4app.json"];
                }
                    break;
                case 5://已逾期
                {
                    [str appendString:@"hope/getborrowouttimeout4app.json"];
                }
                    break;
                    case 7://已流标
                    case 6:
                {
                    [str appendString:@"hope/getborrowoutflow4app.json"];
                }
                    break;
                case 10:
                case 8://已还款
                {
                    [str appendString:@"hope/getborrowoutrepayed4app.json"];
                }
                    break;
                default:
                    break;
            }

        }
            break;
        case 2:
        {
            switch (self.pageType) {
                    case 0:
                    case 11:
                    case 12:
                    case 1://借款中
                {
                    [str appendString:@"hope/getborrowpublish4app.json"];
                }
                    break;
                    case 2://已满标
                    case 700:
                {
                    [str appendString:@"hope/getborrowfull4app.json"];
                }
                    break;
                    case 3:
                    case 13:
                    case 9:
                    case 4://还款中
                {
                    [str appendString:@"hope/getborrowrepaying4app.json"];
                }
                    break;
                case 5://已逾期
                {
                    [str appendString:@"hope/getborrowtimeout4app.json"];
                }
                    break;
                case 7://已流标
                case 6:
                {
                    [str appendString:@"hope/getborrowflow4app.json"];
                }
                    break;
                case 8://已还款
                case 10:
                {
                    [str appendString:@"hope/getborrowrepayed4app.json"];
                }
                    break;
                default:
                    break;
            }

        }
            break;
        default:
            break;
    }
    return str;
}

- (void)fetchBaseInfoData:(void(^)(id output))CB{
    NSString *requestStr = [self fetchBaseInfoRequestStr];
    NSDictionary *params = @{@"hopeid":self.hopeId};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        if (self.pageType ==7||self.pageType==6) {
            if (self.baseType == 0) {
                NSDictionary *againDict = @{@"amount":jsonDict[@"hopeamount"],@"days":jsonDict[@"days"],@"priod":jsonDict[@"priod"],@"hoperate":jsonDict[@"hoperate"],@"reason":jsonDict[@"summary"]};
                [[NSUserDefaults standardUserDefaults] setObject:againDict forKey:@"AGAIN"];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
        }
        ITLogSP(@"%@",jsonDict);
        CB(jsonDict);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
}


- (void)fetchBorrowInfoAndCB:(void(^)(id output))CB{
    NSString *requestStr = [NSString stringWithFormat:@"%@/hope/gethopeinfoapp.json",SERVICE_ADRESS];
    NSDictionary *params = @{@"hopeid":self.hopeId};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        ITLogSP(@"获取借款信息%@",jsonDict);
        CB(jsonDict);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
}

- (void)fetchRepayPlanAndCB:(void(^)(id output))CB{
    NSString *requestStr = [NSString stringWithFormat:@"%@/hope/getrepayplan.json",SERVICE_ADRESS];
    NSDictionary *params = @{@"hopeid":self.hopeId};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        ITLogSP(@"获取还款计划%@",jsonDict);
        CB(jsonDict);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];

}

- (void)fetchBorrowOutAndCB:(void(^)(id output))CB{
    NSString *requestStr = [NSString stringWithFormat:@"%@/hope/getinvestrecord4app.json",SERVICE_ADRESS];
    NSDictionary *params = @{@"hopeid":self.hopeId};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        ITLogSP(@"获取出借记录%@",jsonDict);
        CB(jsonDict);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
    }];

}

- (void)fetchHuiKuanAndCB:(void(^)(id output))CB{
    
    NSString *requestStr = @"";
    NSDictionary *params;
    
    switch (self.pageType) {
//        case 0:
//        case 11:
//        case 12:
//        case 1://借款中
//        case 2://已满标
//        case 700:
//        case 7://已流标
//        case 6:
//        {
//            requestStr = [NSString stringWithFormat:@"%@/hope/genincome.json",SERVICE_ADRESS];
//            params = @{@"hopeid":self.hopeId,@"amount":[[NSUserDefaults standardUserDefaults] objectForKey:@"income"],@"isface":@"true",@"couponsid":@""};
//        }
//            break;
        case 3:
        case 13:
        case 9:
        case 4://还款中
        case 5://已逾期
        case 8://已还款
        case 10:
        {
            requestStr = [NSString stringWithFormat:@"%@/hope/getincomeforapp.json",SERVICE_ADRESS];
            params = @{@"hopeid":self.hopeId};
        }
            break;
        default:
            CB(nil);
            return;
            break;
    }

    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        ITLogSP(@"获取回款计划%@",jsonDict);
        CB(jsonDict);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];

}
- (void)fetchPingLunListAndCB:(void(^)(id output))CB{
    NSString *requestStr = [NSString stringWithFormat:@"%@/hope/gethopereplyforapp.json",SERVICE_ADRESS];
    NSDictionary *params = @{@"hopeid":self.hopeId};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        CB(jsonDict);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];

}
- (void)sendPingLunWithText:(NSString *)str andCB:(void(^)(id output))CB{
    NSString *requestStr = [NSString stringWithFormat:@"%@/hope/savemyhopereply.json",SERVICE_ADRESS];
    NSDictionary *params = @{@"hopeid":self.hopeId,
                             @"content":[self replayRequestStr:str]};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        ITLogSP(@"发布评论%@",jsonDict);
        CB(jsonDict);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];



}



- (void)fetchCanBorrowWithHopeId:(NSString *)hopeId andCB:(void(^)(id output))CB{
    NSString *requestStr = [NSString stringWithFormat:@"%@/hope/getinvestinfo.json",SERVICE_ADRESS];
    NSDictionary *params = @{@"hopeid":hopeId};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        ITLogSP(@"可出借界面获取数据%@",jsonDict);
        CB(jsonDict);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
}

- (NSString *)replayRequestStr:(NSString *)baseStr{
    NSMutableString *reasonMutable = [[NSMutableString alloc] init];
    for (int i=0; i<baseStr.length; i++) {
        UniChar b = [baseStr characterAtIndex:i];
        NSString *str = [ [NSString alloc] initWithFormat:@"%x",b];
        while (str.length<4) {
            str = [NSString stringWithFormat:@"0%@",str];
        }
        [reasonMutable appendString:str];
    }
    return reasonMutable;
}

/**
 *  还款时获取信息
 */
- (void)user_getRepayInfoWithHopeId:(NSString *)hopeID andCB:(void(^)(id output))CB{
    NSString *requestStr = [NSString stringWithFormat:@"%@/user/getrepayinfo4app.json",SERVICE_ADRESS];
    NSDictionary *params = @{@"hopeid":hopeID};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        CB(dict);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
}
/**
 *  获取还款优惠券列表
 */
- (void)hope_getLendCouponsListAndCB:(void(^)(id output))CB{
    NSString *requestStr = [NSString stringWithFormat:@"%@/hope/getlendcouponslist.json",SERVICE_ADRESS];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsondict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        CB(jsondict);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];

}

/**
 *  还款接口
 */
- (void)user_repayHope4appWithHopeID:(NSString *)hopeID andCouponsid:(NSString *)conpousID andCB:(void (^)(id output))CB{
    NSString *requestStr = [NSString stringWithFormat:@"%@/user/repayhope4app.json",SERVICE_ADRESS];
    NSDictionary *params = @{@"hopeid":hopeID,
                             @"couponsid":conpousID,
                             @"channel":@"1",
                             @"flag":@"1"};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsondict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        CB(jsondict);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];


}
@end
